<?php
namespace app\admin\model;

use think\Model;

class Donation extends Model{

//    public function getSubmitStatusAttr($value){
//        $submit_status = [
//            0 => '尚未移交',
//            1 => '成功移交',
//            2 => '书籍退回',
//        ];
//        return $submit_status[$value];
//    }

    /**
     * 获取所有捐赠记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username|do.donation_book' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $data = $this
            ->alias('do')
            ->join('user u', 'do.donation_id=u.id', 'LEFT')
            ->join('user user', 'do.donation_collect_id=user.id', 'LEFT')
            ->field('do.id did,u.full_name,u.tel,do.donation_book,
                   do.donation_price,u.stop_status,do.donation_collect_id,do.get_coin,
                   do.donation_time,do.is_receive,do.submit_status,
                   do.donation_id uid,user.full_name as collect_name,
                   user.tel as collect_tel')
            ->where($where)
            ->order($order)
            ->select();
        if($data){
            foreach($data as $key => $value){
                $data[$key] = $value->toArray();
            }
        }
        return $data;
   }

    /**
     * 获取记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username|do.donation_book' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count = $this
            ->alias('do')
            ->join('user u', 'do.donation_id=u.id', 'LEFT')
            ->join('user user', 'do.donation_collect_id=user.id', 'LEFT')
            ->field('do.id did,u.full_name,u.tel,do.donation_book,
                   do.donation_price,u.stop_status,do.donation_collect_id,do.get_coin,
                   do.donation_time,do.is_receive,do.submit_status,
                   do.donation_id uid,user.full_name as collect_name,
                   user.tel as collect_tel')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 获取捐赠的的详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=""){
        $donation = $this
            ->alias('do')
            ->join('user u', 'do.donation_id=u.id', 'LEFT')
            ->field('do.id,u.full_name,u.tel,do.donation_book,
            do.donation_price,u.stop_status,do.donation_collect_id,
            do.get_coin,do.donation_time,do.is_receive,do.submit_status')
            ->where($where)
            ->find();
        if($donation){
            $donation = $donation->toArray();
        }
        return $donation;
    }


    /**
     * 通过id获取捐赠的的详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getDonationById($id){
        $donation = $this
            ->alias('do')
            ->join('user u', 'do.donation_id=u.id', 'LEFT')
            ->join('user donation_collect_id', 'do.donation_collect_id=donation_collect_id.id', 'LEFT')
            ->field('do.id,do.donation_id,u.full_name,u.tel,do.donation_book,
            do.donation_price,u.stop_status,do.donation_collect_id,
            do.get_coin,do.donation_time,do.is_receive,do.submit_status,
            donation_collect_id.full_name as donation_collect_id')
            ->where('do.id='.$id)
            ->find();
        if($donation){
            $donation = $donation->toArray();
        }
        return $donation;
    }
}